package org.bozo.im.activity.roster;

import java.util.ArrayList;

import org.bozo.im.R;
import org.bozo.im.activity.Constants;
import org.bozo.im.activity.ServiceMessenger;
import org.bozo.im.activity.account.AddAccountDialog;
import org.bozo.im.common.MessageContent;
import org.bozo.im.common.MessageType;
import org.bozo.im.common.types.RawContact;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class RosterActivity extends ListActivity {

	private ServiceMessenger mService;
	private Messenger mMessenger;

	private ContactAdapter mAdapter;
	private LayoutInflater mInflater;

	private AddAccountDialog mAddAccountDialog;

	public RosterActivity() {
		mMessenger = new Messenger(new RosterActivityHandler());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu pMenu) {

		getMenuInflater().inflate(R.menu.contact_list_menu, pMenu);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(int pFeatureId, MenuItem pItem) {

		switch (pItem.getItemId()) {
		case R.id.menu_add_account:

			if (mInflater == null)
				mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View lView = mInflater.inflate(R.layout.add_account, null);

			mAddAccountDialog = new AddAccountDialog(this, mService);
			mAddAccountDialog.setView(lView);
			mAddAccountDialog.setTitle("Add a new account");
			mAddAccountDialog.show();
			break;
		}

		return super.onMenuItemSelected(pFeatureId, pItem);
	}

	private class RosterActivityHandler extends Handler {

		@Override
		public void handleMessage(Message pMsg) {

			pMsg.getData().setClassLoader(getClass().getClassLoader());

			switch (pMsg.what) {
			case MessageType.GET_ROSTER_RESPONSE:

				ArrayList<RawContact> lRoster = new ArrayList<RawContact>();
				ArrayList<Parcelable> lParcelables = pMsg.getData().getParcelableArrayList(MessageContent.CONTACTS);

				for (Parcelable lParcelable : lParcelables)
					if (lParcelable instanceof RawContact)
						lRoster.add((RawContact)lParcelable);

				getAdapter().setList(lRoster);
				break;
			
			default:
				super.handleMessage(pMsg);
			}
		}
	}

	private ServiceConnection mServiceConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName pComponentname, IBinder pIbinder) {

			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.activity.roster.RosterActivity.mServiceConnection.new ServiceConnection() {...}.onServiceConnected ---------- Service connected");
			// ---------------------------------------------------------------------------------------

			mService = new ServiceMessenger(pIbinder, mMessenger);
			mService.registerClient();
			mService.getRoster();
		}
		
		public void onServiceDisconnected(ComponentName pComponentname) {

			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.activity.roster.RosterActivity.mServiceConnection.new ServiceConnection() {...}.onServiceDisconnected ---------- Service disconnected");
			// ---------------------------------------------------------------------------------------
			mService.unregisterClient();
			mService = null;
		}
	};

	@Override
	protected void onStart() {

		super.onStart();

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.activity.contactlist.ContactListView.onCreate ---------- Starting Activity");
		// ---------------------------------------------------------------------------------------

		setListAdapter(getAdapter());

		startService(new Intent("org.bozo.im.service.IMService"));

		bindService(new Intent("org.bozo.im.service.IMService"), mServiceConnection, Context.BIND_AUTO_CREATE);

	}

	@Override
	protected void onStop() {

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.activity.roster.RosterActivity.onDestroy ---------- Stopping activity");
		// ---------------------------------------------------------------------------------------

		unbindService(mServiceConnection);

		super.onDestroy();
	}

	private ContactAdapter getAdapter() {
		if (mAdapter == null)
			mAdapter = new ContactAdapter(this);
		return mAdapter;
	}

	



}
